<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <!-- 添加分类的dialog -->
        <el-button type="primary" @click="addVisible = true" size="mini">添加文章分类</el-button>
        <el-dialog title="提示" @closed="$refs.addForm.resetFields()" :visible.sync="addVisible" width="40%">
          <!-- 表单 -->
          <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="70px" class="demo-addForm">
            <el-form-item label="分类名称" prop="cate_name">
              <el-input v-model="addForm.cate_name"></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
              <el-input v-model="addForm.cate_alias"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addVisible = false" size="mini">取 消</el-button>
            <el-button type="primary" @click="add" size="mini">添 加</el-button>
          </span>
        </el-dialog>

        <!-- 编辑分类的dialog -->
        <el-dialog title="提示" @closed="$refs.editForm.resetFields()" :visible.sync="editVisible" width="40%">
          <!-- 表单 -->
          <el-form :model="editForm" :rules="addRules" ref="editForm" label-width="70px" class="demo-editForm">
            <el-form-item label="分类名称" prop="cate_name">
              <el-input v-model="editForm.cate_name"></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
              <el-input v-model="editForm.cate_alias"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="editVisible = false" size="mini">取 消</el-button>
            <el-button type="primary" @click="edit" size="mini">修 改</el-button>
          </span>
        </el-dialog>

      </div>
      <el-table border :data="list" style="width: 100%">
        <el-table-column prop="id" type="index" label="序号" width="50px">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名">
        </el-table-column>
        <el-table-column label="操作">
          <!-- 作用域插槽，传id 进行文章编辑 -->
          <template v-slot="scope">
            <el-button @click="openEdit(scope.row)" type="perimary" size="mini">编辑</el-button>
            <el-button @click="del(scope.row.id)" type="danger" size="mini">删除</el-button>
          </template>

        </el-table-column>

      </el-table>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'ArtCate',
  data() {
    return {
      list: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return
      // console.log(res)
      this.list = res.data
    },
    add() {
      // 1.兜底校验
      this.$refs.addForm.validate(async vaild => {
        if (!vaild) return
        // 2.发送请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        // 3.提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.重新获取列表
        this.getList()
      })
      // 隐藏dialog
      this.addVisible = false
    },
    openEdit(row) {
      // 阻止用户修改 分类1 2 
      if (row.id == 1 || row.id == 2) return this.$message.error('管理员不允许修改此分类')
      // 将当前点击的数据渲染到编辑dialog的表单中
      // dialog 显示
      this.editVisible = true
      // console.log(row);
      // 使用浅拷贝将数据赋值给表单数据
      this.editForm = { ...row }
    },
    edit() {
      // 1.兜底校验
      this.$refs.editForm.validate(async vaild => {
        if (!vaild) return
        // 2.发送请求
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // 3.提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.重新获取列表
        this.getList()
      })
      // 隐藏dialog
      this.editVisible = false
    },
    del(id) {
      // 拦截用户删除一二分类
      if (id == 1 || id == 2) return this.$message.error('管理员不允许删除此分类')
      this.$confirm('确定删除吗？', '确认删除', { type: "warning" })
        .then(async () => {
          // 2.发送请求
          const { data: res } = await this.$http.delete('/my/cate/del', { params: {id} })
          console.log(res);
          // 3.提醒用户
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 4.重新获取列表
          this.getList()
          // 隐藏dialog
          this.editVisible = false
        })
    }
  },
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
